package kth;

import static org.junit.Assert.assertTrue;
import kth.test.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;

/**
 * This test verifies that PacerEvader moves to the top of the battlefield
 * and moves back and forth along the top of the battlefield.
 * 
 * @author Kimberly Heu
 *
 */
public class TestPacerEvaderMovement extends RobotTestBed {
  
  boolean visitedTop = false; // If PacerEvader went to the top of the battlefield
  boolean paced = false; // If PacerEvader paced
  double lastXCoord; // The last known x-coordinate of PacerEvader
  

  /**
   * Specifies that SittingDuck and PacerEvader are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,kth.PacerEvader";
  }
  
  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 10;
  }
  
  /**
   * At the end of each turn, monitor PacerEvader's movement.
   * Verifies that PacerEvader visited the top of the battlefield and that it
   * moved horizontally on the field, pacing.
   * 
   * @param event the turn that just ended
   */
  @Override 
  public void onTurnEnded(TurnEndedEvent event) {
    // Get and store information on PacerEvader's location.
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double yCoord = robot.getY();
    double xCoord = robot.getX();
    
    // If PacerEvader is at the top of the field, set appropriate boolean true.
    if (yCoord > (fieldHeight - 40)) {
      visitedTop = true;
    }
    
    // If PacerEvader's x-coordinate has changed from what it was last, it is pacing.
    if (lastXCoord != xCoord) {
      paced = true;
    }
    
    // Update PacerEvader's last known x-coordinate.
    lastXCoord = xCoord;
  }
  
  
  /**
   * The actual test, which asserts that PacerEvader both got to the top of the battlefield,
   * and that it moved across the screen.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Made it to top wall", visitedTop);
    assertTrue("Paced", paced);
    
  }
}
